The present invention relates to units in computer programs, and in particular to allowing a computer program to carry units information about its constants, variables, and functions. The units information may be used to introduce conversion factors to ensure that computations in the computer program are carried out in a consistent manner in terms of primary units of the computer program. The present invention relates particularly to a method and apparatus for performing automatic conversion of units used in computer programs.
Computer programs typically include numerous expressions and functions to assign values to variables. These values often relate to units. Examples of such units and dimensions include meters, seconds, cubic meters, inches, kilograms, meters per second, and many more. One of the deficiencies of programming languages is that no information is kept about the units in which the values are expressed. It is taken for granted that programmers will deal with the units appropriately and without formal documentation of the units in the program.
Therefore, automatic verification of consistency of units in a program, say by a compiler, is not possible in known systems. A failure to correctly do so may lead to serious miscalculations. A simple example of this would be if an algebraic expression containing a variable or constant having units of metres per second is added to another variable or constant with units of centimetres per second or inches per second.
According to the article xe2x80x9cNASA Reworks its Sums after Mars Fiascoxe2x80x9d by Reichhardt, T., published in Nature, Vol. 401, Oct. 7, 1999, p. 517, the loss of NASA""s Mars Climate Orbitor on Sep. 23, 1999, as it was about to enter orbit, has been attributed to a confusion between imperial and metric units.
It is an object of the present invention to provide a method of automatic conversion of units in a computer program.
According to a first aspect of the invention, there is provided a method of converting units in a computer program. The method comprises the steps of:
(a1) accepting as input a unit identifier for each variable used in the computer program, wherein each unit identifier comprises one or more unit elements;
(a2) accepting as input a primary unit element from each group of unit elements;
(a3) accepting as input a relation between each of the unit elements and one of the groups of unit elements and a conversion factor to the primary unit element of the group of unit elements;
(d) identifying instances of the variables in the computer program;
(e) augmenting the instances of the variables with a corresponding one of the unit identifiers; and
(f) identifying for each unit element, one of the groups of units and replacing the unit element with the primary unit identifier, multiplied with the conversion factor.
Preferably, when the computer program further includes at least one assignment statement, the method, for each assignment statement, comprises the further step of:
(h) dividing both sides of the assignment statement with the conversion factor(s) on the left hand side of the assignment statement.
Also, when the computer program includes at least one function, the method, for each function, preferably comprises the further steps of:
(a4) accepting as input a unit identifier for the function;
(a5) accepting as input a unit identifier for each parameter of the function;
(b) in a function body of the function, converting units by:
(b1) identifying instances of the variables in the function body;
(b2) augmenting the instances of the variables with a corresponding one of the unit identifiers;
(b3) identifying for each unit element, one of the groups of units and replacing the unit element with the primary unit identifier, multiplied with the conversion factor;
(b4) for each assignment statement in the function body, dividing both sides of the assignment statement with the conversion factor(s) on the left hand side of the assignment statement; and
(b5) dividing a return statement of the function body with conversion factor(s) of the unit identifier for the function;
(c1) identifying instances of arguments in function calls of the function in the computer program;
(c2) augmenting the instances of the arguments with a corresponding one of the unit identifiers; and
(c3) identifying for each unit element, one of the groups of units and replacing the unit element with the primary unit identifier, multiplied with the conversion factor.
(g1) identifying instances of the function call in the computer program;
(g2) augmenting the instances of the function call with a corresponding one of the unit identifiers; and
(g3) identifying for each unit element, one of the groups of units and replacing the unit element with the primary unit identifier, multiplied with the conversion factor.
According to a further aspect of the invention, there is provided an apparatus for converting units in a computer program. The apparatus comprises:
input means for:
accepting as input a unit identifier for each variable used in the computer program, wherein each unit identifier comprise one or more unit elements;
accepting as input a primary unit element from each group of unit elements; and
accepting as input a relation between each of the unit elements and one of the groups of unit elements and a conversion factor to the primary unit element of the group of unit elements;
means for identifying instances of the variables in the computer program;
means for augmenting the instances of the variables with a corresponding one of the unit identifiers; and
means for identifying for each unit element, one of the groups of units and replacing the unit element with the primary unit identifier, multiplied with the conversion factor.